package edu.hubu.service.impl;

import edu.hubu.aspect.ClassCacheEvict;
import edu.hubu.commons.*;
import edu.hubu.dao.impl.IAnnouncementDao;
import edu.hubu.model.Announcement;
import edu.hubu.service.AbstractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class AnnouncementService extends AbstractService<Announcement> {

    @Autowired
    private RedisUtils redisUtil;

    public AnnouncementService(IAnnouncementDao dao) {
        super(dao);
    }

    @Override
    public ResultsJson findByLikePage(int currentPage, int size, Map<String,Object> map) {
        ResultsJson page = super.findByLikePage(currentPage, size, map);
        List<Announcement> users = Object2List.toList(page.getData(), Announcement.class);
        List<Object> collect = users.stream().map(Object2Map::filterMap).collect(Collectors.toList());
        return ResultsJson.toJson(StatusCode.SUCCESS, page.getTotal(), page.getCurrent(), collect);
    }

    @Override
    @Transactional
    public ResultsJson findAllPage(int currentPage, int size) {
        ResultsJson page = super.findAllPage(currentPage, size);
        List<Announcement> users = Object2List.toList(page.getData(), Announcement.class);
        List<Object> collect = users.stream().map(Object2Map::filterMap).collect(Collectors.toList());
        return ResultsJson.toJson(StatusCode.SUCCESS,size,currentPage,collect);
    }


    @ClassCacheEvict
    public ResultsJson updateById(String id, Announcement announcement) {
        return super.updateById(id, announcement,uc -> !uc.isAnnouncementIsDeleted());

    }


    @Override
    @ClassCacheEvict
    public ResultsJson deleteById(String id) {
        return super.deleteById(id);
    }

    @Override
    @ClassCacheEvict
    public ResultsJson add(Announcement announcement) {
        return super.add(announcement);
    }


    public ResultsJson count() {
      return super.count("announcement");
    }


    public boolean clearCache(){
        return redisUtil.deletePrefix("announcement::");
    }

}
